﻿using AutoMapper;
using MediatR;
using MyMES.Domain.zmc;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Commends.Customers;
using MyMES.Read.API.Dtos;
using System.Collections.Generic;

namespace MyMES.Read.API.Application.CommendHandlers.Customers
{
    public class GetCustomerCommendHandlers : IRequestHandler<GetCustomerCommends, APIPaging<List<CustTypeDto>>>
    {

        private readonly IBaseRepository<Customermanagement> customerRepository;
        private readonly IBaseRepository<CustomerTypes> customertypeRepository;
        private readonly IMapper mapper;

        public GetCustomerCommendHandlers(IBaseRepository<Customermanagement> customerRepository, IBaseRepository<CustomerTypes> customertypeRepository, IMapper mapper)
        {
            this.customerRepository = customerRepository;
            this.customertypeRepository = customertypeRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 获取客户信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIPaging<List<CustTypeDto>>> Handle(GetCustomerCommends request, CancellationToken cancellationToken)
        {
            try
            {
                APIPaging<List<CustTypeDto>> result = new APIPaging<List<CustTypeDto>>();
                result.Code = APIEnum.查询成功;
                result.Msg = "查询成功";
                var sql = "SELECT a.*,b.CustomerTypeName FROM customermanagement a LEFT JOIN customertypes b ON a.CustomerTypeId=b.CustomerTypeId  where IsDlete=false ORDER BY a.CustomerId DESC;";


                var custos = await customerRepository.SelectAsync(sql);
                var custypes = await customertypeRepository.SelectAsync(sql);
                var res = mapper.Map<List<CustTypeDto>>(custos);
                var restype = mapper.Map<List<CustTypeDto>>(custypes);


                if (!string.IsNullOrEmpty(request.CustomerNo))
                {
                    res = res.Where(x => x.CustomerNo.Contains(request.CustomerNo)).ToList();
                }
                if (!string.IsNullOrEmpty(request.CustomerName))
                {
                    res = res.Where(x => x.CustomerName.Contains(request.CustomerName)).ToList();
                }
                if (!string.IsNullOrEmpty(request.Customerabbrevias))
                {
                    res = res.Where(x => x.Customerabbrevias.Contains(request.Customerabbrevias)).ToList();
                }
                if (!string.IsNullOrEmpty(request.CustomerEng))
                {
                    res = res.Where(x => x.CustomerEng.Contains(request.CustomerEng)).ToList();
                }

                if (request.Isok != null)
                {
                    var isok = request.Isok == 1 ? true : false;

                    res = res.Where(x => x.Isok == isok).ToList();
                }
                foreach (var item in res)
                {
                    item.CustomerTypeName = restype.FirstOrDefault(x => x.CustomerTypeId == item.CustomerTypeId)?.CustomerTypeName;
                }
                result.TotalCount = res.Count;
                result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
                res = res.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.data = res;
                return result;
            }
            catch (Exception)
            {

                throw;
            }

        }
    }
}
